<?php

namespace Modules\Car\Http\Controllers;

use App\Helpers\CommTrait;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\Maintenance;
use Modules\Car\Entities\RecognizeDrivingLicense;

//车辆保养控制器
class MaintenanceController extends Controller
{
    use ResponseData,PayTrait,CommTrait;
    public function list(Maintenance $model,Request $request){
        $type = $request->input('type','');
        $car_id = $request->input('car_id','');
        $size = $request->input('size',10);
        if($request->filled('search')){
            $name = $request->search;
            $model = $model->where(function($query)use($name){
                $query->where('car_series_name','like','%'.$name.'%')
                    ->orWhere('plate_number','like','%'.$name.'%');
            });
        }
        if($type=='all'){
            $data = $model
                ->with('car','sendPerson','series')
                ->when($car_id,function($query)use($car_id){
                    $query->where('car_id',$car_id);
                })
                ->orderBy('id','desc')->get();
        }else{
            $data = $model
                ->with('car','sendPerson','series')
                ->when($car_id,function($query)use($car_id){
                    $query->where('car_id',$car_id);
                })
                ->orderBy('id','desc')->paginate($size);
        }

        return $this->success($data);
    }
    public function store(Maintenance $maintenance,Request $request){
        $data = $request->all();
        $car = RecognizeDrivingLicense::query()->find($request->car_id);
        if($car){
            $model = $maintenance->query()->create($data);
            if($model){
                if($request->filled('mileage')){
                    $pre0 = $car->total_mileage;
                    $car->last_mileage = $car->total_mileage+$car->mileage-$request->mileage;
                    $car->last_maintenance_mileage = $pre0;
                    $car->total_mileage = $request->mileage;
                }
                if($request->filled('end')){//保养完成
                    $car->use_status = 1;
                    $car->wait_status = 1;
                }else{
                    if($request->need_start>now()){
                        $car->use_status = 11;
                    }else{
                        $car->use_status = 3;
                       // $car->is_used = 1;
                    }
                }
                if($request->filled('spark_plug_mileage')){
                    if($request->spark_plug_mileage>0){
                        $pre1 = $car->spark_plug_mileage;
                        $car->spark_plug_mileage = $request->spark_plug_mileage;
                        $car->spark_plug_last_mileage = $pre1;
                    }
                }
                if($request->filled('gearbox_mileage')){
                    if($request->gearbox_mileage>0){
                        $pre2 = $car->gearbox_mileage;
                        $car->gearbox_mileage = $request->gearbox_mileage;
                        $car->gearbox_plug_last_mileage = $pre2;
                    }
                }
                if($request->filled('spark_plug_change')){
                    $car->gearbox_plug_last_mileage = 0;
                }
                if($request->filled('gearbox_mileage_change')){
                    $car->gearbox_mileage = 0;
                }
                $car->save();
                // 车辆保养统计
                $this->expendStatistatic($car->car_id,$car->price);
            }
            return $this->success($model);
        }else{
            return $this->failed('车辆不存在',201);
        }
    }
    // 更新车辆保养里程数--长租续租用的
    public function storeXuzu(Request $request){
        $car = RecognizeDrivingLicense::query()->find($request->car_id);
        if($car){
            if($request->filled('mileage')){
                $pre1 = $car->spark_plug_mileage;
                $pre2 = $car->gearbox_mileage;
                $spark_plug_mileage = $car->spark_plug_mileage+$request->mileage-$car->total_mileage; // 当前里程+新增里程
                $gearbox_mileage = $car->gearbox_mileage+$request->mileage-$car->total_mileage; // 当前里程+新增里程
                $pre0 = $car->total_mileage;
                $car->last_mileage = $car->total_mileage+$car->mileage-$request->mileage;
                $car->last_maintenance_mileage = $pre0;
                $car->total_mileage = $request->mileage;
                $car->spark_plug_last_mileage = $pre1;
                $car->gearbox_plug_last_mileage = $pre2;
                $car->spark_plug_mileage = $spark_plug_mileage;
                $car->gearbox_mileage = $gearbox_mileage;
                $car->save();
            }
            return $this->success('更新成功');
        }else{
            return $this->failed('车辆不存在',201);
        }
    }

    public function edit(Maintenance $maintenance,Request $request){
        $data = $maintenance->find($request->id);
        return $this->success($data);
    }
    public function update(Maintenance $maintenance,Request $request){
        $data = $maintenance->query()->find($request->id);
        if ($request->filled('car_id')) {
            $car = RecognizeDrivingLicense::query()->find($request->car_id);
            if($car){
                $data->update($request->all());
                if($request->filled('mileage')){
                    $car->last_mileage = $car->total_mileage+$car->mileage-$request->mileage;
                    $car->last_maintenance_mileage = $request->mileage;
                }
                if($request->filled('spark_plug_mileage')){
                    if($request->spark_plug_mileage>0){
                        $car->spark_plug_mileage = $request->spark_plug_mileage;
                    }
                }
                if($request->filled('gearbox_mileage')){
                    if($request->gearbox_mileage>0){
                        $car->gearbox_mileage = $request->gearbox_mileage;
                    }
                }
                if($request->filled('spark_plug_change')){
                    $car->gearbox_plug_last_mileage = 0;
                }
                if($request->filled('gearbox_mileage_change')){
                    $car->gearbox_mileage = 0;
                }
                if($request->filled('end')){//保养完成
                    $car->use_status = 1;
                    $car->wait_status = 1;
                    // $car->is_used = 0;
                    $this->confirmForbidDate($data->car_id,3);
                }
                $car->save();
                return $this->success('修改成功');
            }else{
                return $this->failed('车辆不存在',201);
            }
        } else {
            $map = $request->all();
            $data->update($map);
            return $this->success('修改成功');
        }
    }
    public function destory(Maintenance $maintenance,Request $request){
        $data = $maintenance->find($request->id);
        $data->delete();
    }
}
